package com.qf.servlet;

import com.qf.domain.Book;
import com.qf.domain.PageBean;
import com.qf.service.BookService;
import com.qf.service.impl.BookServiceImpl;
import com.qf.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * projectName:day14_booksystem_page
 * author:liuXin
 * time:2022/9/30 9:35
 * description:
 */
@WebServlet("/booklistpage")
public class BookListPageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        //1获取前端数据
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");

        //后加
        String title = request.getParameter("title");
        String author = request.getParameter("author");

        //组织SQL
        StringBuilder where=new StringBuilder(" where 1=1 ");
        List<Object> params=new ArrayList<Object>();
        if(!StringUtils.isEmpty(title)){
            where.append(" and title like ? ");
            params.add("%"+title+"%");
        }
        if(!StringUtils.isEmpty(author)){
            where.append(" and author like ? ");
            params.add("%"+author+"%");
        }




        int page_num=1;
        int page_size=8;
        try {
            if(!StringUtils.isEmpty(pageNum)){
                page_num=Integer.parseInt(pageNum);
                if(page_num<1){
                    page_num=1;
                }
            }
            if(!StringUtils.isEmpty(pageSize)){
                page_size=Integer.parseInt(pageSize);
                if(page_size<1){
                    page_size=8;
                }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        //2调用业务逻辑
        try {
            BookService bookService=new BookServiceImpl();
//            PageBean<Book> pageBean= bookService.findByPage(page_num,page_size);
            //后加
            PageBean<Book> pageBean= bookService.findByPage(page_num,page_size,where.toString(),params);
            System.out.println("pageBean = " + pageBean);
            //3转发重定向
            request.setAttribute("pageBean",pageBean);

            //后加
            request.setAttribute("title",title);
            request.setAttribute("author",author);
            request.getRequestDispatcher("/booklistpage.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            //3转发重定向
            request.setAttribute("msg","查询失败"+e.getMessage());
            request.getRequestDispatcher("/message.jsp").forward(request,response);
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}
